package com.seven.asimov;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class main extends Activity {
	/** Called when the activity is first created. */
	private Timer writeLog;
	private FileOutputStream outputStream;
	private FileOutputStream telstateOutputStream;
	private Button btn_stop;
	private Button btn_start;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn_start = (Button) findViewById(R.id.btn_start);
		btn_start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				writeToFile();
			}
		});

		btn_stop = (Button) findViewById(R.id.btn_stop);
		btn_stop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				while (doWriteLine.cancel())
					;
				writeLog.cancel();
				try {
					outputStream.flush();
					telstateOutputStream.flush();
					outputStream.close();
					telstateOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

	}

	private TimerTask doWriteLine = new TimerTask() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				Date date = new Date(System.currentTimeMillis());
				SimpleDateFormat foo = new SimpleDateFormat(
						"yyyy-MM-dd hh:mm:ss.SSS");
				foo.setTimeZone(TimeZone.getTimeZone("UTC"));
				String strDate = foo.format(date);
				outputStream.write(generateLogLine(strDate).getBytes());
				outputStream.flush();
				telstateOutputStream.write(generatetelstateLogLine(strDate)
						.getBytes());
				telstateOutputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};

	private void writeToFile() {
		try {
			deleteFile("netlog.txt");
			deleteFile("telstate.txt");
			outputStream = openFileOutput("netlog.txt", Activity.MODE_PRIVATE
					| Activity.MODE_WORLD_READABLE);
			telstateOutputStream = openFileOutput("tel_state.txt",
					Activity.MODE_PRIVATE | Activity.MODE_WORLD_READABLE);
			writeLog = new Timer();
			writeLog.schedule(doWriteLine, 0, 500);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private String generateLogLine(String date) {
		String line = "";
		// 2011-03-20 02:13:19.890
		// UTC,56,242,446,206,511,434,seven.com,hotmail,background,proxy_uncacheable,tcp,mobile

		Random random = new Random();
		int nServerIn = random.nextInt(1000);
		int nServerOut = random.nextInt(1000);
		int nClientIn = random.nextInt(1000);
		int nClientOut = random.nextInt(1000);
		int nCacheIn = random.nextInt(1000);
		int nCacheOut = random.nextInt(1000);
		String strTimeStamp = date + " UTC";

		line += strTimeStamp;
		line += "," + nClientIn;
		line += "," + nClientOut;
		line += "," + nServerIn;
		line += "," + nServerOut;
		line += "," + nCacheIn;
		line += "," + nCacheOut;
		line += ",seven.com,hotmail,background,proxy_uncacheable,tcp,mobile\n";
		return line;
	}

	private String generatetelstateLogLine(String date) {
		String line = "";
		// 2011-03-20 02:13:19.890
		// UTC,56,242,446,206,511,434,seven.com,hotmail,background,proxy_uncacheable,tcp,mobile
		String[] states = { "DATA_CONNECTED", "DATA_ACTIVITY_DORMANT",
				"DATA_ACTIVITY_CONNECTED" };
		int length = states.length;
		Random random = new Random();
		String state = states[random.nextInt(length)];
		String strTimeStamp = date;
		int bytes = random.nextInt(1000);

		line += strTimeStamp;
		line += "," + state;
		line += "," + bytes;
		line += "\n";
		return line;
	}
}